<?xml version="1.0" encoding="UTF-8"?>

<!--
    Document   : products.xsl
    Created on : March 31, 2013, 3:12 PM
    Author     : Win7
    Description:
        Purpose of transformation follows.
-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:product="http://xml.netbeans.org/schema/products"
    xmlns:cart="http://xml.netbeans.org/schema/shopcarts"
    exclude-result-prefixes="product cart"
    version="1.0">
    <xsl:output method="html" indent="yes"/>
    <xsl:param name="paramSearch" select="''"/>
    <xsl:param name="propath" select="''"/>
    <xsl:param name="userJSP" select="''"/>
    <xsl:param name="type" select="''"/>
    <xsl:param name="cartList" select="''"/>
    <xsl:param name="contextpath" select="''"/>
    <xsl:param name="checkUsername" select="''"/>
    <xsl:template match="/">
        <xsl:if test="$type='searchProducts'">
            <xsl:call-template name="insearch"/>
        </xsl:if>
        <xsl:if test="$type='empty'">
            <xsl:call-template name="empty"/>
        </xsl:if>
        <xsl:if test="$type='cartView'">
            <xsl:call-template name="cartView"/>
        </xsl:if>
    </xsl:template>
    <xsl:template name="empty">
        <table border="1" class="table table-hover">
            <tr>
                <th>Id</th>
                <th>Name</th>
                <th>Image</th>
                <th>Description</th>
                <th>Quantity</th>
                <th>Price</th>
                <xsl:if test="$checkUsername!=''">
                    <th></th>
                </xsl:if>                
            </tr>
            <xsl:for-each select="product:Products/product:Product">
                <tr>
                    <td>
                        <xsl:value-of select="product:Id"/>
                    </td>
                    <td>
                        <xsl:value-of select="product:Name"/>
                    </td>
                    <td>
                        <img>
                            <xsl:attribute name="src">
                                <xsl:value-of select="$contextpath"/>
                                <xsl:text>/</xsl:text>
                                <xsl:value-of select="product:ImageURL"/>
                            </xsl:attribute>
                        </img>
                    </td>
                    <td>
                        <xsl:value-of select="product:Description"/>
                    </td>
                    <td>
                        <xsl:value-of select="product:Quantity"/>
                    </td>
                    <td>
                        <xsl:value-of select="product:Price"/>
                    </td>
                    <xsl:if test="$checkUsername!=''">
                        <td>
                            <div class="btn-group dropup">
                                <a>
                                    <xsl:attribute name="proID">
                                        <xsl:value-of select="product:Id"/>
                                    </xsl:attribute>
                                    <xsl:attribute name="proName">
                                        <xsl:value-of select="product:Name"/>
                                    </xsl:attribute>
                                    <xsl:attribute name="proPrice">
                                        <xsl:value-of select="product:Price"/>
                                    </xsl:attribute>
                                    <xsl:attribute name="href">
                                        <xsl:text>#</xsl:text>
                                    </xsl:attribute>
                                    <xsl:attribute name="class">
                                        <xsl:text>btn btn-success linkAddToCart</xsl:text>
                                    </xsl:attribute>
                                    <xsl:text>Add Cart</xsl:text>
                                </a>
                                <button class="btn btn-info dropdown-toggle" data-toggle="dropdown">
                                    <span class="caret"></span>
                                </button>
                                <ul class="dropdown-menu">
                                    <li>
                                        <a>
                                            <xsl:attribute name="proID">
                                                <xsl:value-of select="product:Id"/>
                                            </xsl:attribute>
                                            <xsl:attribute name="href">
                                                <xsl:text>#</xsl:text>
                                            </xsl:attribute>
                                            <xsl:attribute name="class">
                                                <xsl:text>btn btn-info linkComment</xsl:text>
                                            </xsl:attribute>
                                            <xsl:text>Comment</xsl:text>
                                        </a>
                                    </li>
                                </ul>
                            </div>
                        </td>
                    </xsl:if>
                </tr>
            </xsl:for-each>
        </table>
    </xsl:template>

    <xsl:template name="insearch">
        <table border="1" class="table table-hover">
            <tr>
                <th>Id</th>
                <th>Name</th>
                <th>Image</th>
                <th>Description</th>
                <th>Quantity</th>
                <th>Price</th>
                <xsl:if test="$checkUsername!=''">
                    <th/>
                </xsl:if>
            </tr>
            <xsl:for-each select="product:Products/product:Product[contains(product:Name, $paramSearch)]">
                <tr>
                    <td>
                        <xsl:value-of select="product:Id"/>
                    </td>
                    <td>
                        <xsl:value-of select="product:Name"/>
                    </td>
                    <td>
                        <img>
                            <xsl:attribute name="src">
                                <xsl:value-of select="$contextpath"/>
                                <xsl:text>/</xsl:text>
                                <xsl:value-of select="product:ImageURL"/>
                            </xsl:attribute>
                        </img>
                    </td>
                    <td>
                        <xsl:value-of select="product:Description"/>
                    </td>
                    <td>
                        <xsl:value-of select="product:Quantity"/>
                    </td>
                    <td>
                        <xsl:value-of select="product:Price"/>
                    </td>
                    <xsl:if test="$checkUsername!=''">
                        <td>
                            <div class="btn-group dropup">
                                <a>
                                    <xsl:attribute name="proID">
                                        <xsl:value-of select="product:Id"/>
                                    </xsl:attribute>
                                    <xsl:attribute name="proName">
                                        <xsl:value-of select="product:Name"/>
                                    </xsl:attribute>
                                    <xsl:attribute name="proPrice">
                                        <xsl:value-of select="product:Price"/>
                                    </xsl:attribute>
                                    <xsl:attribute name="href">
                                        <xsl:text>#</xsl:text>
                                    </xsl:attribute>
                                    <xsl:attribute name="class">
                                        <xsl:text>btn btn-success linkAddToCart</xsl:text>
                                    </xsl:attribute>
                                    <xsl:text>Add Cart</xsl:text>
                                </a>
                                <button class="btn btn-info dropdown-toggle" data-toggle="dropdown">
                                    <span class="caret"></span>
                                </button>
                                <ul class="dropdown-menu">
                                    <li>
                                        <a>
                                            <xsl:attribute name="proID">
                                                <xsl:value-of select="product:Id"/>
                                            </xsl:attribute>
                                            <xsl:attribute name="href">
                                                <xsl:text>#</xsl:text>
                                            </xsl:attribute>
                                            <xsl:attribute name="class">
                                                <xsl:text>btn btn-info linkComment</xsl:text>
                                            </xsl:attribute>
                                            <xsl:text>Comment</xsl:text>
                                        </a>
                                    </li>

                                </ul>
                            </div>
                        </td>
                    </xsl:if>
                </tr>
            </xsl:for-each>
        </table>
    </xsl:template>

    <xsl:template name="cartView1">
        <xsl:variable name="prodoc" select="document($propath)"/>
        <table border="1">
            <tr>
                <th>Id</th>
                <th>Name</th>
                <th>Quantity</th>
                <th>Price</th>
                <th/>
            </tr>
            <xsl:for-each select="cart:Carts/cart:Cart">
                <xsl:variable name="cartId" select="cart:Id"/>
                <xsl:variable name="productNode" select="$prodoc/product:Products/product:Product[product:Id=$cartId]"/>
                <tr>
                    <td>
                        <xsl:value-of select="$productNode/product:Id"/>
                    </td>
                    <td>
                        <xsl:value-of select="$productNode/product:Name"/>
                    </td>
                    <td>
                        <input type="text" name="txtQuantity" value="1"/>
                    </td>
                    <td>
                        <xsl:value-of select="$productNode/product:Price"/>
                    </td>
                    <td>
                        <a>
                            <xsl:attribute name="proID">
                                <xsl:value-of select="product:Id"/>
                            </xsl:attribute>
                            <xsl:attribute name="proName">
                                <xsl:value-of select="product:Name"/>
                            </xsl:attribute>
                            <xsl:attribute name="proPrice">
                                <xsl:value-of select="product:Price"/>
                            </xsl:attribute>
                            <xsl:attribute name="href">
                                <xsl:text>#</xsl:text>
                            </xsl:attribute>
                            <xsl:attribute name="class">
                                <xsl:text>btn btn-success removeCart</xsl:text>
                            </xsl:attribute>
                            <xsl:text>Remove</xsl:text>
                        </a>
                    </td>
                </tr>
            </xsl:for-each>
        </table>
    </xsl:template>


    <xsl:template name="cartView">
        <div class="cart-header">
            <div class="cart-info-container">
                Contain:
                <xsl:choose>
                    <xsl:when test="cart:Order/cart:Count">
                        <xsl:value-of select="cart:Order/cart:Count"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:text>0</xsl:text>
                    </xsl:otherwise>
                </xsl:choose>
                product in your shopping cart!!
            </div>
            <div class="spacer" style="clear: both;"></div>
        </div>
        <xsl:if test="count(cart:Order/cart:Cart) &gt; 0">
            <div id="cart-items-container">
                <xsl:for-each select="cart:Order/cart:Cart">
                    <div productid="{cart:ProductId}" class="cart-item">
                        <p class="cart-item-name">
                            <xsl:value-of select="cart:ProductName"/>
                        </p>
                        <p class="cart-item-xx">x</p>
                        <input type="text" min="1" class="cart-item-quantity" value="{cart:Quantity}"/>
                        <span class="cart-item-total">
                            <xsl:value-of select="cart:TotalPrice"/>
                        </span>
                         <img src="{$contextpath}/res/img/deletebutton.png" alt="Remove Product" class="cart-item-delete-btn"/>
                    </div>
                </xsl:for-each>
                <div class="spacer" style="clear: both;"></div>
            </div>
        </xsl:if>

        <div class="total-payment-container">
            <span class="total-payment-sum-title">Total Price: </span>
            <span class="total-payment-sum-value">
                <xsl:value-of select="sum(cart:Order/cart:Cart/cart:TotalPrice)"/>
            </span>
            <a class="btn btn-success btnCheckOut">Check Out</a>
        </div>
    </xsl:template>
    
</xsl:stylesheet>
